<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\teacher\Teacher;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $classList = [];
    public function _initialize()
    {
        //分类
        $this->classList = model('\app\admin\model\allclass\Allclass')->getClassList();
    }

    /**
     * 首页
     *
     */
    public function index()
    {   

        //分类ID
        $classid = $this->request->request('classid');

        $where = array();
        $where['status'] = 0;
        if(!empty($classid)){
            $where['classid'] = $classid;
        }

        //查询咨询师
        $teacher_list = db('teacher')->where($where)->field('id,name,nickname,classid,tags,headimg,sort')->order('sort', 'desc')->limit(3)->select();

        if(!empty($teacher_list)){
            foreach($teacher_list as $key=>$value){
                $teacher_list[$key]['classid_title'] = model("\app\admin\model\allclass\Allclass")->getClassidInfo($value['classid']);//分类
                $teacher_list[$key]['tags_title'] = model("\app\admin\model\allclass\Tags")->get_tags($value['tags']);//标签

                $teacher_list[$key]['order_count'] = 0;//咨询数量
                $teacher_list[$key]['praise_rate'] = "0%";//好评率
                $teacher_list[$key]['start_price'] = "$0起";//咨询起价

                $teacher_list[$key]['headimg'] = getImagesUrl($value['headimg']);
            }
        }

        //录播课
        $course_list = db('course')->where($where)->order('sort', 'desc')->limit(3)->select();
        if(!empty($course_list)){
            foreach($course_list as $key=>$value){
                $course_list[$key]['classid_title'] =model("\app\admin\model\allclass\Allclass")->get_class_name($value['classid']);//分类
                $course_list[$key]['cover'] = getImagesUrl($value['cover']);
            }
        }

        //文章
        $article_list = db('article')->where($where)->order('sort', 'desc')->limit(3)->select();
        if(!empty($article_list)){
            foreach($article_list as $key=>$value){
                $article_list[$key]['classid_title'] =model("\app\admin\model\allclass\Allclass")->get_class_name($value['classid']);//分类
                $article_list[$key]['author'] = $this->getAuthorInfo($value['author']);//作者
                $article_list[$key]['readnum'] = 0;//阅读量
                $article_list[$key]['cover'] = getImagesUrl($value['cover']);
            }
        }

        //分类列表
        $data = ['classList'=>$this->classList,'teacher_list'=>$teacher_list,'course_list'=>$course_list,'article_list'=>$article_list];
        $this->success('请求成功',$data);
    }



    /**
    * 获取文章作者 
    */
    public function getAuthorInfo($id = ""){
        
        $author = "";

        if($id == 0){
           $author = "平台";
        }else{
            $teacher = new Teacher();
            $author = $teacher->get_teacher_nickname($id);
        }

        return $author;
    }


    /**
     * 轮播图
     * 
     */

    public function banner(){
        
        //轮播图
        $banner_list = db('banner')->where('status', 0)->field('id,url,item_url,sort')->order('sort', 'asc')->select();
        
        if(!empty($banner_list)){

            foreach($banner_list as $key=>$value){
                if(empty($value['url'])){
                    unset($banner_list[$key]);
                }else{
                    $banner_list[$key]['url'] =  getImagesUrl($value['url']);
                }

            }
        }

        $this->success(__('Logged in successful'), $banner_list);

    }
}
